import numpy as np
import csv
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_dist(x1, y1, x2, y2):
    temp_dist = (float(x1 - x2))**2 + (float((y1 - y2))**2)
    #print x1, y1, x2, y2, (float(x1 - x2))**2, (float((y1 - y2))**2), temp_dist
    temp_dist = temp_dist**0.5
    #print temp_dist

    return temp_dist

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    #filepath = 'C:/_DATA/migration_census_2000/REDCAPLUS_25Region/'
    #filepath = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/'
    filepath = 'C:/_DATA/migration_census_2000/state/'

    #dataCSV = filepath + 'migration_100regs_dist.csv'
    dataCSV = filepath + 'census_migration_state_dist.csv'
    #dataCSV = filepath + 'census_migration_100regs_dis.csv'
    data = build_data_list(dataCSV)

    print data

    #file = filepath + 'census_migration_100regs_pop.csv'
    #file = filepath + '100reg_pop.csv'
    file = filepath + 'census_migration_state_pop2000.csv'
    pop = build_data_list(file)
    
    #grossflowCSV = filepath + 'migration_100regs_inoutflow.csv'
    grossflowCSV = filepath + 'census_migration_state_inoutflow.csv'
    #grossflowCSV = filepath + 'census_migration_100regs_inoutflow.csv'
    grossflow = build_data_list(grossflowCSV)

    inflow = np.zeros(len(grossflow))
    outflow = np.zeros(len(grossflow))

    for item in data:
        outflow[item[1]] += item[-1]
        inflow[item[2]] += item[-1]

    print zip(inflow, outflow)
        
    output = []
    
    for item in data:
        #dist = int(cal_dist(centroid[item[0], 0], centroid[item[0], 1], centroid[item[1], 0], centroid[item[1], 1]))
        output.append([item[0], grossflow[item[1], 1], grossflow[item[2], 0], pop[item[1], 1], pop[item[2], 0], item[3]])

    #print output
    headerstr = 'dist,f1,f2,p1,p2,flowVol'
    np.savetxt(filepath + '100reg_regressiondata1.csv', output, delimiter=',', header = headerstr, fmt = '%s')